package com.javainterface.service;

import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户管理service层
 */
@Service
public class UserCentorService {
    @Autowired
    RestTemplate restTemplate;

    public String getToken(){
        String loginId="portalapi";
        String secretKey="T2@3op9W374";
//        String url="http://10.0.12.198:18000/bim-server/api/rest/management/ExtApiMgmtAuthService/login";
        String url="https://im.behc.com.cn/bim-server/api/rest/management/ExtApiMgmtAuthService/login";
        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/x-www-form-urlencoded");
        //添加参数
        MultiValueMap<String,Object> params=new LinkedMultiValueMap<>();
        params.add("loginId",loginId);
        params.add("secretKey",secretKey);
        //九九归一
        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(params, headers);

        String ret=restTemplate.postForObject(url,httpEntity,String.class);
        JSONObject jsonObject=JSONObject.fromObject(ret);
        String token=jsonObject.getString("data");
        return token;
    }

    public String passwordUpdate(String token,String username,String oldPassword,String newPassword){
//        String url="http://10.0.12.198:18000/bim-server/api/rest/management/ExtApiMgmtUserService/changePasswordByUsernameAndPassword";
        String url="https://im.behc.com.cn/bim-server/api/rest/management/ExtApiMgmtUserService/changePasswordByUsernameAndPassword";
        // 设置请求头
        HttpHeaders headers = new HttpHeaders();

        headers.add("Content-Type", "application/json; charset=UTF-8");
        //添加参数
        Map<String,Object> params=new HashMap<>();
        params.put("token",token);
        params.put("username",username);
        params.put("oldPassword",oldPassword);
        params.put("newPassword",newPassword);

        //九九归一
        HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(params, headers);

        String ret=restTemplate.postForObject(url,httpEntity,String.class);
        return ret;

    }
    public String passwordReset(String token,String userName,String newPassword){
//        String url="http://10.0.12.198:18000/bim-server/api/rest/management/ExtApiMgmtUserService/changePasswordByUsername";
        String url="https://im.behc.com.cn/bim-server/api/rest/management/ExtApiMgmtUserService/changePasswordByUsername";
        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json; charset=UTF-8");
        //添加参数
        Map<String,Object> params=new HashMap<>();
        params.put("token",token);
        params.put("username",userName);
        params.put("newPassword",newPassword);

        //九九归一
        HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(params, headers);

        String ret=restTemplate.postForObject(url,httpEntity,String.class);
        return ret;

    }
}
